﻿using JuCloudy.Framework.Interface;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.SystemVariable
{
    public partial class MainPage : UserControl,IModule<JuCloudy.Data.Entity.SysVariable>
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += (obj, e) =>
            {
                OnLoad();
            };
        }

        private JuCloudy.Framework.Interface.IStructure Structure =
                   (JuCloudy.Framework.Interface.IStructure)Application.Current.RootVisual;

        private ObservableCollection<JuCloudy.Data.Entity.SysVariable> entities
        {
            get { return new ObservableCollection<Data.Entity.SysVariable>((IEnumerable<Data.Entity.SysVariable>)this._dg.ItemsSource); }
            set { this._dg.ItemsSource = value; }
        }

        private void InitData()
        {
            this.entities = (ObservableCollection<JuCloudy.Data.Entity.SysVariable>)JuCloudy.Framework.Storge.VariableHelper.ItemSource;
        }

        public void refresh()
        {
            Structure.ShowLoading();
            SystemVariable.SystemVariableSoapClient client
                = new SystemVariable.SystemVariableSoapClient();
            client.GetEntitiesAsync();
            client.GetEntitiesCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysVariable>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysVariable>;
            };
        }

        public void insert(JuCloudy.Data.Entity.SysVariable entity)
        {
            Structure.ShowLoading();
            SystemVariable.SystemVariableSoapClient client
                = new SystemVariable.SystemVariableSoapClient();
            client.InsertAsync(entity.Serialize());
            client.InsertCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysVariable>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysVariable>;
                Structure.CloseChildDialog();
            };
        }

        public void delete(JuCloudy.Data.Entity.SysVariable entity)
        {
            Structure.ShowLoading();
            SystemVariable.SystemVariableSoapClient client
                = new SystemVariable.SystemVariableSoapClient();
            client.DeleteAsync(entity.Serialize());
            client.DeleteCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysVariable>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysVariable>;
                Structure.CloseChildDialog();
            };
        }

        private void OnLoad()
        {
            this._insertbtn.Click += (obj, e) =>
            {
                var dialog = new controls.Insert();
                dialog.Mainpage = this;
                Structure.ShowChildDialog(dialog, "新增变量");
            };

            this._updatabtn.Click += (obj, e) =>
            {
                var dialog = new controls.Insert();
                dialog.Mainpage = this;
                dialog.Entity = this._dg.SelectedItem as JuCloudy.Data.Entity.SysVariable;
                Structure.ShowChildDialog(dialog, "编辑变量");
            };

            this._dg.SelectionChanged += _dg_SelectionChanged;
        }

        private void _dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                this._updatabtn.IsEnabled = true;
                this._deletebtn.IsEnabled = true;
            }
            else
            {
                this._updatabtn.IsEnabled = false;
                this._deletebtn.IsEnabled = false;
            }
        }

        public void InitResource()
        {
            SystemVariable.SystemVariableSoapClient client
                = new SystemVariable.SystemVariableSoapClient();
            client.GetEntitiesAsync();
            client.GetEntitiesCompleted += (obj, ex) =>
            {
                Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                JuCloudy.Framework.Storge.VariableHelper.ItemSource = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysVariable>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysVariable>;
                InitData();
            };
        }
    }
}
